package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.my_order_list.chlid

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.Album
import com.example.library_common.album.bean.AlbumFile
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.shushiyunsst.R
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class DrawbackAddImgFragment : SimpleListFragment<AlbumFile>(), OnItemChildClickListener {


    companion object{
        fun newInstance(): DrawbackAddImgFragment {
            val args = Bundle()
            val fragment = DrawbackAddImgFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var isAddFooter = false

    private var maxCount = 3
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isAddDividerItemDecoration = false
        isAlwaysRefresh = false
        isEnableLoadMore = false
        isInitialRefresh = false

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mSwipeRefreshLayout.isEnabled = false

        addFooter(view)
        mAdapter.footerViewAsFlow = false
        mAdapter.addChildClickViewIds(R.id.cancel)
        mAdapter.setOnItemChildClickListener(this)


    }

    private fun addFooter(view: View) {
        if (isAddFooter) {
            return
        }

        isAddFooter = true

        val footer = LayoutInflater.from(_mActivity)
            .inflate(R.layout.draw_back_add_img_fragment_footer, view as ViewGroup, false)
        mAdapter.removeAllFooterView()
        mAdapter.setFooterView(footer)

        footer?.setOnClickListener {

            Album.getInstance()
                .multipleImageAlbum()
                .setMaxSelectCount(maxCount - mAdapter.data.size)
                .onFailed { _, msg ->
                    Toast.makeText(_mActivity, msg, Toast.LENGTH_LONG).show()
                }
                .onSuccess {
                    mAdapter.addData(it)
                    if (mAdapter.data.size >= maxCount) {
                        mAdapter.removeAllFooterView()
                        isAddFooter = false
                    }
                }
                .execute(childFragmentManager)
        }
    }

    fun getSelector() = mAdapter.data

    override fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false)
    }

    override fun getItemLayout(): Int {
        return R.layout.draw_back_add_img_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: AlbumFile?) {
        helper?.setImageLocalUrl(R.id.addImg, item?.path)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(AlbumFile::class.java).endSubType().build()
    }

    override fun netRequest() {

    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (view.id) {
            R.id.cancel -> {
                mAdapter.removeAt(position)
                addFooter(rootView)
            }
        }
    }


}